import { Injectable } from '@angular/core';
import {Router} from '@angular/router'
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class AuthGuard implements CanActivate {
  constructor (
    private router: Router
  ){}
  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    const userToken = window.localStorage.getItem('userToken');
    if (!userToken) {
      console.log('gg');
      this.router.navigate(['/signin']);
      return false // 不能继续导航
    }
    // 如果验证通过，则放行，继续完成导航
    return true;
  }
}
